import { defineStore } from 'pinia'

const THEME_KEY = 'app_theme'

export const useThemeStore = defineStore('theme', {
  state: () => ({
    current: localStorage.getItem(THEME_KEY) || 'light',
    themes: [
      { id: 'light', name: '浅色' },
      { id: 'dark', name: '深色' },
      { id: 'blue', name: '蓝色' },
      { id: 'green', name: '绿色' },
    ],
  }),
  actions: {
    apply(theme) {
      this.current = theme
      localStorage.setItem(THEME_KEY, theme)
      try {
        document.documentElement.setAttribute('data-theme', theme)
      } catch {}
    },
    init() {
      this.apply(this.current)
    }
  }
})
